package com.xfatm.killer;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.xfatm.killer.entity.MainPageAdapter;
import com.xfatm.killer.entity.MainTabEnum;
import com.xfatm.killer.entity.TabFragment;
import com.xfatm.killer.ui.BaseActivity;
import com.xfatm.killer.util.Activitys;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        /*ListView listView=findViewById(R.id.allAppList);
        listView.setAdapter(new AppInfoAdapter(listView.getContext(),getAppInfos()));*/
    }

    private void init() {
        TabLayout tabLayout = findViewById(R.id.mainTab);
        ViewPager viewPager = findViewById(R.id.viewPage);
        //初始化tab标签
        List<TabFragment> fragments = new ArrayList<>();
        for (MainTabEnum tabEnum : MainTabEnum.values()) {
            tabLayout.addTab(tabLayout.newTab().setText(tabEnum.getTitle()));
            fragments.add(TabFragment.newInstance(tabEnum));
        }

        viewPager.setAdapter(new MainPageAdapter(getSupportFragmentManager(), fragments));
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitTime = System.currentTimeMillis() - exitTime;
            if (exitTime > 1000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                exitTime=System.currentTimeMillis();
            } else if (exitTime > 0 && exitTime < 1000) {
                Activitys.finishAll();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}